<!--  -->
<template>
  <el-container class="home_container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="~@/assets/images/heima.png"
             alt="">
        <span>JASON后台管理系统</span>
      </div>
      <el-button type="info"
                 @click="logOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <!-- 侧边栏是否折叠 -->
        <div class="toggle-button"
             @click="toggleCollapse">|||</div>

        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#434def"
                 unique-opened
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 :router="true"
                 :default-active="activePath"
                 @click="toggleCollapse">

          <!-- 点击下面的选项都是一起打开或者关闭，原因是submenu里面的index是一样的，index值需要唯一的字符串 -->
          <!-- 一级菜单 -->
          <el-submenu :index="menu.id+''"
                      v-for="(menu,index) in menuList"
                      :key="menu.id">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <i :class="iconFontObj[menu.id]"></i>
              <span>{{menu.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subMenu.path"
                          v-for="(subMenu,index) in menu.children"
                          :key="subMenu.id"
                          @click="saveNavState('/'+subMenu.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subMenu.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>

        <!-- 侧边栏菜单区域 -->
        <!-- <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409EFF"
                 unique-opened>
          <el-submenu :index="item.id + ''"
                      v-for="item in menuList"
                      :key="item.id">
            <template slot="title">
              <i :class="iconFontObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu> -->
      </el-aside>
      <!-- 内容名主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { reqMenuList } from '@/api/index'
export default {
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      // iconfont对象
      iconFontObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-dingdan',
        145: 'iconfont icon-shuju'
      },
      // 是否水平收放
      isCollapse: false,
      // 活跃路径
      activePath: ''
    }
  },
  methods: {
    // 1.登出账户
    logOut () {
      sessionStorage.removeItem('TOKEN')
      this.$router.push('/login')
    },
    // 2.获取左侧菜单数据
    async getMenuList () {
      const { data: res } = await reqMenuList()
      console.log(res)
      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
    },
    // 3.是否水平收紧菜单
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 4.保持链接的状态
    saveNavState (activePath) {
      sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
    // 5.
  },
  // 生命周期 - 创建完成（访问当前this实例）
  created () {

  },
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted () {
    this.getMenuList()
    this.activePath = sessionStorage.getItem('activePath')
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}

.el-header {
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  font-size: 20px;

  > div {
    display: flex;
    justify-content: space-around;
    align-items: center;
    img {
      margin-right: 20px;
    }
  }
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 20px;
  line-height: 40px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
